import React from "react";
import Button from 'antd/lib/button';
import notification from "@/services/notification";
import { get } from "lodash";
import Uploads from "@/services/uploads";
import Upload from "antd/lib/upload";
import 'antd/dist/antd.less'
import { UploadOutlined } from '@ant-design/icons';
import Attaches from "@/services/attaches";

export default class EmployeeAttache extends React.Component {
  state = {
    loading: true,
    annex0: [],//--其他附件
    annex1: [],//--证书(执业资格证或职称证)
    annex2: [],//--毕业证
    annex3: [],//--身份证(正面)
    annex4: [],//--身份证(反面)
    annex5: [],//--寸照（不限底色）
    annex6: [],//--评审表
    annex7: [],//--发文（红头文件）
    annex8: [],//--学籍在线认证报告
  };
  componentWillMount() {
    this.annexadd();
  }
  annexadd =()=> {
    Attaches.get({ sourceid: this.props.id, type: "employee" }).then(data => {
      let annex0 = [];//--附件
      let annex1 = [];//--附件
      let annex2 = [];//--附件
      let annex3 = [];//--附件
      let annex4 = [];//--附件
      let annex5 = [];//--附件
      let annex6 = [];//--附件
      let annex7 = [];//--附件
      let annex8 = [];//--附件

      data.annex.forEach(file => {
        if (Number(file.types) === 1) {
          if (annex1.length < 1) {
            annex1 = [file];
          } else {
            annex1.push.apply(annex1, [file]);
          }
        } else if (Number(file.types) === 2) {
          if (annex2.length < 1) {
            annex2 = [file];
          } else {
            annex2.push.apply(annex2, [file]);
          }
        } else if (Number(file.types) === 3) {
          if (annex3.length < 1) {
            annex3 = [file];
          } else {
            annex3.push.apply(annex3, [file]);
          }
        } else if (Number(file.types) === 4) {
          if (annex4.length < 1) {
            annex4 = [file];
          } else {
            annex4.push.apply(annex4, [file]);
          }
        } else if (Number(file.types) === 5) {
          if (annex5.length < 1) {
            annex5 = [file];
          } else {
            annex5.push.apply(annex5, [file]);
          }
        } else if (Number(file.types) === 6) {
          if (annex6.length < 1) {
            annex6 = [file];
          } else {
            annex6.push.apply(annex6, [file]);
          }
        } else if (Number(file.types) === 7) {
          if (annex7.length < 1) {
            annex7 = [file];
          } else {
            annex7.push.apply(annex7, [file]);
          }
        } else if (Number(file.types) === 8) {
          if (annex8.length < 1) {
            annex8 = [file];
          } else {
            annex8.push.apply(annex8, [file]);
          }
        } else {
          if (annex0.length < 1) {
            annex0 = [file];
          } else {
            annex0.push.apply(annex0, [file]);
          }
        }
      });
      this.setState({
        annex0: annex0,//--附件
        annex1: annex1,//--附件
        annex2: annex2,//--附件
        annex3: annex3,//--附件
        annex4: annex4,//--附件
        annex5: annex5,//--附件
        annex6: annex6,//--附件
        annex7: annex7,//--附件
        annex8: annex8,//--附件
      });
    }).catch(error => {
    });
  }
  handledown(file) {
    let filename = file.url;
    let names = file.name;
    if (names.split('.')[1] !== filename.split('.')[1]) {
      filename = filename.split('.')[0] + '.' + names.split('.')[1];
    }
    let AjaxUrl = 'api/upload/downloadfile';
    fetch(AjaxUrl, {
      method: 'POST',
      body: window.JSON.stringify({ filename: filename }),
      credentials: 'include',
      headers: new Headers({
        'Content-Type': 'application/json'
      })
    }).then((response) => {
      response.blob().then(blob => {
        const aLink = document.createElement('a');
        document.documentElement.appendChild(aLink);
        aLink.style.display = 'none';
        const objectUrl = window.URL.createObjectURL(blob);
        aLink.href = objectUrl;
        aLink.download = names;
        aLink.click();
        document.documentElement.removeChild(aLink);
      });
    }).catch((error) => {
      window.console.log(error);
    });
  };
  getPdfURL(types) {
    const _this = this;
    const props = {
      name: 'file',
      action: 'api/upload/uploadfile/employee/' + _this.props.id + '/' + types,
      headers: {
        authorization: 'authorization-text',
      },
      onChange(info) {
        let fileList = [...info.fileList];
        var newd = [];
        fileList.forEach(file => {
          if (file.response) {
            file.url = file.response.filename;
          }
          const b = [{
            uid: file.uid,
            name: file.name,
            types: types,
            status: file.status,
            url: file.url
          }];
          if (newd.length < 1) {
            newd = b;
          } else {
            newd.push.apply(newd, b);
          }
          if (types === 1) {
            _this.setState({ annex1: newd });
          } else if (types === 2) {
            _this.setState({ annex2: newd });
          } else if (types === 3) {
            _this.setState({ annex3: newd });
          } else if (types === 4) {
            _this.setState({ annex4: newd });
          } else if (types === 5) {
            _this.setState({ annex5: newd });
          } else if (types === 6) {
            _this.setState({ annex6: newd });
          } else if (types === 7) {
            _this.setState({ annex7: newd });
          } else if (types === 8) {
            _this.setState({ annex8: newd });
          } else {
            _this.setState({ annex0: newd });
          }
        });
      },
      onDownload: _this.handledown,
      onRemove(infos) {
        let uid = infos.uid;
        const id = _this.props.id;
        Uploads.removefile({ sourcetype: "employee", sourceid: id, uid: uid }).then(data => {
          if (data.code === "sucess") {
            notification.success(data.msg);
            _this.annexadd();
          }
          else {
            notification.error(data.msg);
          }
        }).catch(error => {
          notification.error(get(error, "response.data.message", "删除失败."));
        });
      },
      showUploadList: {
        showDownloadIcon: true,
        downloadIcon: '下载',
        showRemoveIcon: true,
        removeIcon: '删除',
        // removeIcon: <DeleteOutlined onClick={e => console.log(e, 'custom removeIcon event')} />,
      },
    };
    return props;
  };
  fieldedit(field) {
    if (!this.props.isedit) {
      return true;
    }
    if (this.props.colcontrol) {
      if (this.props.allow_col.indexOf(field) >= 0) {
        return false;
      } else {
        return true;
      }
    } else {
      return false;
    }
  }
  // ===================================zhuijiafujian===================
  render() {
    return (
      <div>
        <table border="1px"
          style={{ tableLayout: "fixed", wordBreak: "break-all", wordWrap: "break-word" }}
          width={document.querySelector('body').offsetWidth * 0.7}>
          <tr><td><p>基本(状态为签约时必填)</p></td></tr>
          <tr>
            <td>
              <Upload {...this.getPdfURL(1)} fileList={this.state.annex1} disabled={this.fieldedit("annex")}>
                <Button disabled={this.fieldedit("annex")}>
                  <UploadOutlined /> 证书(执业资格证或职称证)
                </Button>
              </Upload>
            </td>
            <td>
              <Upload {...this.getPdfURL(2)} fileList={this.state.annex2} disabled={this.fieldedit("annex")}>
                <Button disabled={this.fieldedit("annex")}>
                  <UploadOutlined /> 毕业证
                </Button>
              </Upload>
            </td>
          </tr>
          <tr><td>
            <Upload {...this.getPdfURL(3)} fileList={this.state.annex3} disabled={this.fieldedit("annex")}>
              <Button disabled={this.fieldedit("annex")}>
                <UploadOutlined /> 身份证(正面)
              </Button>
            </Upload>
          </td><td>
              <Upload {...this.getPdfURL(4)} fileList={this.state.annex4} disabled={this.fieldedit("annex")}>
                <Button disabled={this.fieldedit("annex")}>
                  <UploadOutlined /> 身份证(反面)
                </Button>
              </Upload>
            </td></tr>
          <tr><td>
            <Upload {...this.getPdfURL(5)} fileList={this.state.annex5} disabled={this.fieldedit("annex")}>
              <Button disabled={this.fieldedit("annex")}>
                <UploadOutlined /> 寸照（不限底色）
              </Button>
            </Upload>

          </td></tr>
          <tr><td><p>其他</p></td></tr>
          <tr><td>
            <Upload {...this.getPdfURL(6)} fileList={this.state.annex6} disabled={this.fieldedit("annex")}>
              <Button disabled={this.fieldedit("annex")}>
                <UploadOutlined /> 评审表
              </Button>
            </Upload>
          </td><td>
              <Upload {...this.getPdfURL(7)} fileList={this.state.annex7} disabled={this.fieldedit("annex")}>
                <Button disabled={this.fieldedit("annex")}>
                  <UploadOutlined /> 发文（红头文件）
                </Button>
              </Upload>
            </td></tr>
          <tr><td>
            <Upload {...this.getPdfURL(8)} fileList={this.state.annex8} disabled={this.fieldedit("annex")}>
              <Button disabled={this.fieldedit("annex")}>
                <UploadOutlined /> 学籍在线认证报告
              </Button>
            </Upload>
          </td><td>
              <Upload {...this.getPdfURL(0)} fileList={this.state.annex0} disabled={this.fieldedit("annex")}>
                <Button disabled={this.fieldedit("annex")}>
                  <UploadOutlined /> 其他附件
                </Button>
              </Upload>
            </td></tr>
        </table>
      </div>
    );
  };
}

